package com.docking.heylo.web.controller;

import com.docking.heylo.ThreadContext;
import com.docking.heylo.constant.SystemConstant;
import com.docking.heylo.dao.entity.PersonalInformation;
import com.docking.heylo.pojo.Aperture;
import com.docking.heylo.service.ICacheService;
import com.docking.heylo.service.IPostService;
import com.docking.heylo.service.IUserService;
import com.docking.heylo.utils.HeyloMessageSource;
import com.docking.heylo.web.dto.ApertureDto;
import com.docking.heylo.web.utils.ResultUtil;
import com.docking.heylo.web.vo.BaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/***
 * 光圈
 */
@RestController
@RequestMapping("/aperture")
public class ApertureController {

    @Autowired
    private HeyloMessageSource ms;

    @Autowired
    private ICacheService cacheService;

    @Autowired
    private IPostService postService;

    @Autowired
    private IUserService userService;

    /***
     * 首页
     *
     */
    @PostMapping("/index")
    public BaseVo index(@RequestBody ApertureDto dto){
        ThreadContext.authorize.get().setLat(dto.getLat());
        ThreadContext.authorize.get().setLon(dto.getLon());

        List<Aperture> apertures = cacheService.apertureIndex();
        userService.syncPositioning(new PersonalInformation(){{
            setLat(dto.getLat());
            setLon(dto.getLon());
        }});
        return ResultUtil.success(ms.getMessage(SystemConstant.success), apertures);
    }

    /***
     * 作品列表
     *
     * @return
     */
    @PostMapping("/posts")
    public BaseVo posts(){
        List<Map<String, Object>> posts = postService.my_buddy_post();
        return ResultUtil.success(ms.getMessage(SystemConstant.success), posts);
    }


    @PostMapping("/load")
    public BaseVo load(){
        return ResultUtil.success("成功", postService.load());
    }

}
